// Copyright (c) 2024，D-Robotics.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/***************************************************************************
* COPYRIGHT NOTICE
* Copyright 2019 Horizon Robotics, Inc.
* All rights reserved.
***************************************************************************/
#ifndef UTILITY_SENSOR_INC_SC230AI_SETTING_H_
#define UTILITY_SENSOR_INC_SC230AI_SETTING_H_

#ifdef __cplusplus
extern "C" {
#endif

/* more data for sc230ai
*
*       如果需要 sc230ai 作为 master，输出 帧同步信号，给其他 sensor 做同步，
*       可能会用到下面的信息。
        0x300a,0x24, //bit[2]=1 fsync_oen
        0x3032,0xa0, //bit[7]=1 vsync_tc_en
        0x3225,0x30, //bit[4]=1 vsync_end_man_en
        {0x3216,0x3217,}上升沿位置  默认为 0x00 0x02，单位为 1行
        {0x322e，0x322f}下降沿位置  默认为 0x00 0x02，单位为 1行

        修改：
        //vsync_end_man_en
        i2ctransfer -y -f 3 w3@0x32 0x32 0x25 0x30
        实测测量 30fps，默认为低电平，高电平时间为 118us
        //修改下降沿位置
        i2ctransfer -y -f 3 w3@0x32 0x32 0x2f 0x03
        默认为低电平，高电平时间为 148us
        //修改下降沿位置
        i2ctransfer -y -f 3 w3@0x32 0x32 0x2f 0x04
        默认为低电平，高电平时间为 178us
        30fps 代码里, lines_per_second = 33750, 则 每行曝光时间为 1s/33750 = 29.6 us
        符合预期
 * */

static uint32_t sc230ai_linear_30fps_init_setting[] = {
        0x0103,0x01,
        0x0100,0x00,
        0x36e9,0x80,
        0x37f9,0x80,
        0x300a,0x24,
        0x3018,0x12,
        0x3019,0x0e,
        0x301f,0x62,
        0x3032,0xa0,
        0x320c,0x09,
        0x320d,0x60,
        0x3225,0x30, //vsync_end_man_en
        0x3301,0x07,
        0x3304,0x50,
        0x3306,0x70,
        0x3308,0x18,
        0x3309,0x68,
        0x330a,0x01,
        0x330b,0x20,
        0x3314,0x15,
        0x331e,0x41,
        0x331f,0x59,
        0x3333,0x10,
        0x3334,0x40,
        0x335d,0x60,
        0x335e,0x06,
        0x335f,0x08,
        0x3364,0x5e,
        0x337c,0x02,
        0x337d,0x0a,
        0x3390,0x01,
        0x3391,0x0b,
        0x3392,0x0f,
        0x3393,0x09,
        0x3394,0x0d,
        0x3395,0x60,
        0x3396,0x48,
        0x3397,0x49,
        0x3398,0x4b,
        0x3399,0x07,
        0x339a,0x0a,
        0x339b,0x0d,
        0x339c,0x60,
        0x33a2,0x04,
        0x33ad,0x2c,
        0x33af,0x40,
        0x33b1,0x80,
        0x33b3,0x40,
        0x33b9,0x0a,
        0x33f9,0x78,
        0x33fb,0xa0,
        0x33fc,0x4f,
        0x33fd,0x5f,
        0x349f,0x03,
        0x34a6,0x4b,
        0x34a7,0x5f,
        0x34a8,0x30,
        0x34a9,0x20,
        0x34aa,0x01,
        0x34ab,0x28,
        0x34ac,0x01,
        0x34ad,0x58,
        0x34f8,0x7f,
        0x34f9,0x10,
        0x3614,0x01,
        0x3630,0xc0,
        0x3632,0x54,
        0x3633,0x44,
        0x363b,0x20,
        0x363c,0x08,
        0x3651,0x7f,  //0x10
        0x3670,0x09,
        0x3674,0xb0,
        0x3675,0x80,
        0x3676,0x88,
        0x367c,0x40,
        0x367d,0x49,
        0x3690,0x33,
        0x3691,0x33,
        0x3692,0x43,
        0x369c,0x49,
        0x369d,0x4f,
        0x36ae,0x4b,
        0x36af,0x4f,
        0x36b0,0x87,
        0x36b1,0x9b,
        0x36b2,0xb7,
        0x36d0,0x01,
        0x36ea,0x09,
        0x36eb,0x0c,
        0x36ec,0x0c,
        0x36ed,0x24,
        0x3722,0x97,
        0x3724,0x22,
        0x3728,0x90,
        0x37fa,0x09,
        0x37fb,0x32,
        0x37fc,0x10,
        0x37fd,0x34,
        0x3901,0x02,
        0x3902,0xc5,
        0x3904,0x04,
        0x3907,0x00,
        0x3908,0x41,
        0x3909,0x00,
        0x390a,0x00,
        0x3933,0x84,
        0x3934,0x0a,
        0x3940,0x64,
        0x3941,0x00,
        0x3942,0x04,
        0x3943,0x0b,
        0x3e00,0x00,
        0x3e01,0x2a,
        0x3e02,0x20,
	0x3e06,0x00,
	0x3e07,0xa6,
	0x3e09,0x40,
        0x440e,0x02,
        0x450d,0x11,
        0x4819,0x0a,
        0x481b,0x06,
        0x481d,0x16,
        0x481f,0x05,
        0x4821,0x0b,
        0x4823,0x05,
        0x4825,0x05,
        0x4827,0x05,
        0x4829,0x09,
        0x5010,0x01,
        0x5787,0x08,
        0x5788,0x03,
        0x5789,0x00,
        0x578a,0x10,
        0x578b,0x08,
        0x578c,0x00,
        0x5790,0x08,
        0x5791,0x04,
        0x5792,0x00,
        0x5793,0x10,
        0x5794,0x08,
        0x5795,0x00,
        0x5799,0x06,
        0x57ad,0x00,
        0x5ae0,0xfe,
        0x5ae1,0x40,
        0x5ae2,0x3f,
        0x5ae3,0x38,
        0x5ae4,0x28,
        0x5ae5,0x3f,
        0x5ae6,0x38,
        0x5ae7,0x28,
        0x5ae8,0x3f,
        0x5ae9,0x3c,
        0x5aea,0x2c,
        0x5aeb,0x3f,
        0x5aec,0x3c,
        0x5aed,0x2c,
        0x5af4,0x3f,
        0x5af5,0x38,
        0x5af6,0x28,
        0x5af7,0x3f,
        0x5af8,0x38,
        0x5af9,0x28,
        0x5afa,0x3f,
        0x5afb,0x3c,
        0x5afc,0x2c,
        0x5afd,0x3f,
        0x5afe,0x3c,
        0x5aff,0x2c,
        0x36e9,0x53,
        0x37f9,0x53,
//       0x0100,0x01,  //strem off first
};


static uint32_t sc230ai_linear_30fps_slave_mode_init_setting[] = {
        0x0103,0x01,
        0x0100,0x00,
        0x36e9,0x80,
        0x37f9,0x80,
        // 0x300a,0x24,
        0x3018,0x12,
        0x3019,0x0e,
        0x301f,0x62,
        // 0x3032,0xa0,
        0x320c,0x09,
        0x320d,0x60,
        // 0x3225,0x30, //vsync_end_man_en
        0x3301,0x07,
        0x3304,0x50,
        0x3306,0x70,
        0x3308,0x18,
        0x3309,0x68,
        0x330a,0x01,
        0x330b,0x20,
        0x3314,0x15,
        0x331e,0x41,
        0x331f,0x59,
        0x3333,0x10,
        0x3334,0x40,
        0x335d,0x60,
        0x335e,0x06,
        0x335f,0x08,
        0x3364,0x5e,
        0x337c,0x02,
        0x337d,0x0a,
        0x3390,0x01,
        0x3391,0x0b,
        0x3392,0x0f,
        0x3393,0x09,
        0x3394,0x0d,
        0x3395,0x60,
        0x3396,0x48,
        0x3397,0x49,
        0x3398,0x4b,
        0x3399,0x07,
        0x339a,0x0a,
        0x339b,0x0d,
        0x339c,0x60,
        0x33a2,0x04,
        0x33ad,0x2c,
        0x33af,0x40,
        0x33b1,0x80,
        0x33b3,0x40,
        0x33b9,0x0a,
        0x33f9,0x78,
        0x33fb,0xa0,
        0x33fc,0x4f,
        0x33fd,0x5f,
        0x349f,0x03,
        0x34a6,0x4b,
        0x34a7,0x5f,
        0x34a8,0x30,
        0x34a9,0x20,
        0x34aa,0x01,
        0x34ab,0x28,
        0x34ac,0x01,
        0x34ad,0x58,
        0x34f8,0x7f,
        0x34f9,0x10,
        0x3614,0x01,
        0x3630,0xc0,
        0x3632,0x54,
        0x3633,0x44,
        0x363b,0x20,
        0x363c,0x08,
        0x3651,0x7f,  //0x10
        0x3670,0x09,
        0x3674,0xb0,
        0x3675,0x80,
        0x3676,0x88,
        0x367c,0x40,
        0x367d,0x49,
        0x3690,0x33,
        0x3691,0x33,
        0x3692,0x43,
        0x369c,0x49,
        0x369d,0x4f,
        0x36ae,0x4b,
        0x36af,0x4f,
        0x36b0,0x87,
        0x36b1,0x9b,
        0x36b2,0xb7,
        0x36d0,0x01,
        0x36ea,0x09,
        0x36eb,0x0c,
        0x36ec,0x0c,
        0x36ed,0x24,
        0x3722,0x97,
        0x3724,0x22,
        0x3728,0x90,
        0x37fa,0x09,
        0x37fb,0x32,
        0x37fc,0x10,
        0x37fd,0x34,
        0x3901,0x02,
        0x3902,0xc5,
        0x3904,0x04,
        0x3907,0x00,
        0x3908,0x41,
        0x3909,0x00,
        0x390a,0x00,
        0x3933,0x84,
        0x3934,0x0a,
        0x3940,0x64,
        0x3941,0x00,
        0x3942,0x04,
        0x3943,0x0b,
        0x3e00,0x00,
        0x3e01,0x8c,
        0x3e02,0x10,
        0x440e,0x02,
        0x450d,0x11,
        0x4819,0x0a,
        0x481b,0x06,
        0x481d,0x16,
        0x481f,0x05,
        0x4821,0x0b,
        0x4823,0x05,
        0x4825,0x05,
        0x4827,0x05,
        0x4829,0x09,
        0x5010,0x01,
        0x5787,0x08,
        0x5788,0x03,
        0x5789,0x00,
        0x578a,0x10,
        0x578b,0x08,
        0x578c,0x00,
        0x5790,0x08,
        0x5791,0x04,
        0x5792,0x00,
        0x5793,0x10,
        0x5794,0x08,
        0x5795,0x00,
        0x5799,0x06,
        0x57ad,0x00,
        0x5ae0,0xfe,
        0x5ae1,0x40,
        0x5ae2,0x3f,
        0x5ae3,0x38,
        0x5ae4,0x28,
        0x5ae5,0x3f,
        0x5ae6,0x38,
        0x5ae7,0x28,
        0x5ae8,0x3f,
        0x5ae9,0x3c,
        0x5aea,0x2c,
        0x5aeb,0x3f,
        0x5aec,0x3c,
        0x5aed,0x2c,
        0x5af4,0x3f,
        0x5af5,0x38,
        0x5af6,0x28,
        0x5af7,0x3f,
        0x5af8,0x38,
        0x5af9,0x28,
        0x5afa,0x3f,
        0x5afb,0x3c,
        0x5afc,0x2c,
        0x5afd,0x3f,
        0x5afe,0x3c,
        0x5aff,0x2c,
        0x36e9,0x53,
        0x37f9,0x53,
        // clear setting
        0x3222, 0x00,
        0x3230, 0x00,
        0x3231, 0x04,
        0x3224, 0x82,
        0x300a, 0x20,
        0x3032, 0x20,
        // set slave mode
        0x3222, 0x01,
        0x3230, 0x00,
        0x3231, 0x04,
        0x3224, 0x82, // 0x82: efsync; 0x92: fsync
        0x320f, 0x64, // 30fps时的配置，让sensor内部的频率与trigger信号同步
//       0x0100,0x01,  //strem off first
};


static uint32_t sc230ai_stream_on_setting[] = {
	0x0100,0x01,
};

static uint32_t sc230ai_stream_off_setting[] = {
	0x0100,0x00,
};

#if 0
typedef struct sc230ai_gain_value_s
{
        uint32_t again;
        uint32_t dgain;
        uint32_t dfgain;
} sc230ai_gain_value_t;
#endif

/* index = 32*log2(gain);
 * isp calculate gain, and driver will receive index.
 *
 * for this sc230ai sensor, we only use one gain lut:
 * gain_value = again * d_gain(1x-2x), and if d_gain < 2.0, we only use d_fine_gain
 * {again, dgain, dfgain}, //[index, gain_value]=again*dgain
*/
static uint32_t sc230ai_gain_lut[] =
{
        0x000080,       //1.0x
        0x000083,
        0x000086,
        0x000089,
        0x00008c,
        0x00008f,
        0x000092,
        0x000095,
        0x000099,
        0x00009c,
        0x00009f,
        0x0000a3,
        0x0000a7,
        0x0000aa,
        0x0000ae,
        0x0000b2,
        0x0000b5,
        0x0000b9,
        0x0000be,
        0x0000c2,
        0x0000c6,
        0x0000ca,
        0x0000cf,
        0x0000d3,
        0x0000d8,
        0x0000dd,
        0x0000e1,
        0x0000e6,
        0x0000eb,
        0x0000f0,
        0x0000f6,
        0x0000fb,
        0x010080,       //2.0x
        0x010083,
        0x010086,
        0x010089,
        0x01008c,
        0x01008f,
        0x010092,
        0x010095,
        0x010099,
        0x01009c,
        0x01009f,
        0x0100a3,
        0x0100a7,
        0x0100aa,
        0x0100ae,
        0x0100b2,
        0x0100b5,
        0x0100b9,
        0x0100be,
        0x0100c2,
        0x0100c6,
        0x400083,
        0x400085,
        0x400088,
        0x40008b,
        0x40008e,
        0x400092,
        0x400095,
        0x400098,
        0x40009b,
        0x40009f,
        0x4000a2,
        0x4000a6,       //4.0x
        0x4000a9,
        0x4000ad,
        0x4000b1,
        0x4000b5,
        0x4000b9,
        0x4000bd,
        0x4000c1,
        0x4000c5,
        0x4000c9,
        0x4000ce,
        0x4000d2,
        0x4000d7,
        0x4000db,
        0x4000e0,
        0x4000e5,
        0x4000ea,
        0x4000ef,
        0x4000f4,
        0x4000fa,
        0x4000ff,
        0x480083,
        0x480085,
        0x480088,
        0x48008b,
        0x48008e,
        0x480092,
        0x480095,
        0x480098,
        0x48009b,
        0x48009f,
        0x4800a2,
        0x4800a6,       //8.0x
        0x4800a9,
        0x4800ad,
        0x4800b1,
        0x4800b5,
        0x4800b9,
        0x4800bd,
        0x4800c1,
        0x4800c5,
        0x4800c9,
        0x4800ce,
        0x4800d2,
        0x4800d7,
        0x4800db,
        0x4800e0,
        0x4800e5,
        0x4800ea,
        0x4800ef,
        0x4800f4,
        0x4800fa,
        0x4800ff,
        0x490083,
        0x490085,
        0x490088,
        0x49008b,
        0x49008e,
        0x490092,
        0x490095,
        0x490098,
        0x49009b,
        0x49009f,
        0x4900a2,
        0x4900a6,       //16.0x
        0x4900a9,
        0x4900ad,
        0x4900b1,
        0x4900b5,
        0x4900b9,
        0x4900bd,
        0x4900c1,
        0x4900c5,
        0x4900c9,
        0x4900ce,
        0x4900d2,
        0x4900d7,
        0x4900db,
        0x4900e0,
        0x4900e5,
        0x4900ea,
        0x4900ef,
        0x4900f4,
        0x4900fa,
        0x4900ff,
        0x4b0083,
        0x4b0085,
        0x4b0088,
        0x4b008b,
        0x4b008e,
        0x4b0092,
        0x4b0095,
        0x4b0098,
        0x4b009b,
        0x4b009f,
        0x4b00a2,
        0x4b00a6,       //32.0x
        0x4b00a9,
        0x4b00ad,
        0x4b00b1,
        0x4b00b5,
        0x4b00b9,
        0x4b00bd,
        0x4b00c1,
        0x4b00c5,
        0x4b00c9,
        0x4b00ce,
        0x4b00d2,
        0x4b00d7,
        0x4b00db,
        0x4b00e0,
        0x4b00e5,
        0x4b00ea,
        0x4b00ef,
        0x4b00f4,
        0x4b00fa,
        0x4b00ff,
        0x4f0083,
        0x4f0085,
        0x4f0088,
        0x4f008b,
        0x4f008e,
        0x4f0092,
        0x4f0095,
        0x4f0098,
        0x4f009b,
        0x4f009f,
        0x4f00a2,
        0x4f00a6,       //64.0x
        0x4f00a9,
        0x4f00ad,
        0x4f00b1,
        0x4f00b5,
        0x4f00b9,
        0x4f00bd,
        0x4f00c1,
        0x4f00c5,
        0x4f00c9,
        0x4f00ce,
        0x4f00d2,
        0x4f00d7,
        0x4f00db,
        0x4f00e0,
        0x4f00e5,
        0x4f00ea,
        0x4f00ef,
        0x4f00f4,
        0x4f00fa,
        0x4f00ff,
        0x5f0083,
        0x5f0085,
        0x5f0088,
        0x5f008b,
        0x5f008e,
        0x5f0092,
        0x5f0095,
        0x5f0098,
        0x5f009b,
        0x5f009f,
        0x5f00a2,
        0x5f00a6,       //128.0x
        0x5f00a9,
        0x5f00ad,
        0x5f00b1,
        0x5f00b5,
        0x5f00b9,
        0x5f00bd,
        0x5f00c1,
        0x5f00c5,
        0x5f00c9,
        0x5f00ce,
        0x5f00d2,
        0x5f00d7,
        0x5f00db,
        0x5f00e0,
        0x5f00e5,
        0x5f00ea,
        0x5f00ef,
        0x5f00f4,
        0x5f00fa,
        0x5f00ff,
        0x5f0183,
        0x5f0185,
        0x5f0188,
        0x5f018b,
        0x5f018e,
        0x5f0192,
        0x5f0195,
        0x5f0198,
        0x5f019b,
        0x5f019f,       //isp will limit, index = 254, gain max: 245.146
        0x5f01a2,       //255, 99.2x2.531=251
};

#ifdef __cplusplus
}
#endif

#endif  //UTILITY_SENSOR_INC_SC230AI_SETTING_H_
